import mountElement from "./mountElement"

import createDomElement from './createDomElement'
import unmountNode from "./unmountNode"

export default function mountNativeElement(virtualDOM, container, oldDOM) { 
  let newElement = createDomElement(virtualDOM)
  if (oldDOM) {
    container.insertBefore(newElement, oldDOM)
  } else { 
    container.appendChild(newElement)
  }
  // 判断旧的 DOM 对象是否存在，存在则删除
  if (oldDOM) {
    unmountNode(oldDOM)
  }

  const component = virtualDOM.component

  if (component) {
    component.setDOM(newElement)
  }
}